// by Xeno

if (isNil "paramsArray") then {
	GVAR(hasParamsArray) = false;
	if (isClass (missionConfigFile/"Params")) then {
		_conf = missionConfigFile/"Params";
		for "_i" from 0 to (count _conf - 1) do {
			_paramName = configName (_conf select _i);
			_paramval = getNumber (_conf/_paramName/"default");
			if (_paramval != -99999) then {
				__mNsSetVar [_paramName, _paramval];
			};
		};
	};
} else {
	GVAR(hasParamsArray) = true;
	for "_i" from 0 to (count paramsArray - 1) do {
		_paramval = paramsArray select _i;
		if (_paramval != -99999) then {
			__mNsSetVar [configName ((missionConfigFile/"Params") select _i), _paramval];
		};
	};
};

#ifdef __TT__
GVAR(WithJumpFlags) = 1;
GVAR(MaxNumAmmoboxes) = GVAR(MaxNumAmmoboxes) * 2;
#endif
GVAR(WithBackpack) = (GVAR(WithBackpack) == 0);
GVAR(LimitedWeapons) = (GVAR(LimitedWeapons) == 0);
GVAR(WithChopHud) = (GVAR(WithChopHud) == 0);
GVAR(with_ranked) = (GVAR(with_ranked) == 0);
GVAR(reload_engineoff) = (GVAR(reload_engineoff) == 0);

// TYRGHEN - Isn't loading properly otherwise
GVAR(p_marker) = (getArray (missionConfigFile/"Params"/QGVAR(MarkerTypeL)/"texts")) select GVAR(MarkerTypeL);
GVAR(p_marker_dirs) = (GVAR(p_marker_dirs) == 0);
GVAR(v_marker_dirs) = (GVAR(v_marker_dirs) == 0);

GVAR(random_sm_array) = (GVAR(random_sm_array) == 0);

GVAR(with_mgnest) = (GVAR(with_mgnest) == 0);
GVAR(with_medtent) = (GVAR(with_medtent) == 0);
GVAR(WithAcre) = (GVAR(WithAcre) == 0);
#ifndef __TT__
GVAR(with_ai) = (GVAR(with_ai) == 0);
#else
GVAR(with_ai) = false;
GVAR(WithRecapture) = 1;
#endif

GVAR(number_targets_h) = GVAR(MainTargets);

if (isNil QGVAR(MTPriorityObjective)) then { GVAR(MTPriorityObjective) = 1 };
if (isNil QGVAR(MTPriorityObjectivethresold)) then { GVAR(MTPriorityObjectivethresold) = 0 };
if (isNil QGVAR(with_mine_field)) then { GVAR(with_mine_field) = 1 };
if (isNil QGVAR(with_mine_roads)) then { GVAR(with_mine_roads) = 1 };
if (isNil QGVAR(WithEnemyFireBase)) then { GVAR(WithEnemyFireBase) = 1 };
if (isNil QGVAR(teleport_in_town)) then { GVAR(teleport_in_town) = 0 };

if (GVAR(MainTargets) >= 50) then {
	_h = switch (GVAR(MainTargets)) do {
		case 50: {7};
		case 60: {5};
		case 70: {8};
		case 90: {33};
	};
	GVAR(MainTargets) = _h;
};

if (GVAR(GrasAtStart) == 1) then {setTerrainGrid 50};

if (isServer) then {
	// TYRGHEN - We only update the time when the start parameters have been properly synched
	if (GVAR(hasParamsArray)) then { skipTime GVAR(TimeOfDay); };
	["currentTime", DayTime] call FUNC(NetSetJIP);
};

GVAR(version) = [];
#define __adddv(dtype) GVAR(version) set [count GVAR(version), #dtype]
__adddv(A3);
if (GVAR(with_ai)) then {__adddv(AI)};
#ifdef __TT__
__adddv(TT);
#endif
if (GVAR(with_ranked)) then {__adddv(RANKED)};
if (GVAR(WithRevive) == 0) then {__adddv(REVIVE)};

if (isNil QGVAR(with_ai_features)) then {GVAR(with_ai_features) = 1};

if (isServer || {!isDedicated && {!hasInterface}}) then {
	// first element (array. for example: [2,1]): number of vehicle groups that will get spawned, the first number is the max number that will get spawned,
	// the second one the minimum. So [2,0] means, there can be no vehicle groups at all or a maximum of 2 groups of this kind
	// second element: maximum number of vehicles in group; randomly chosen
	switch (GVAR(WithLessArmor)) do {
		case 0: {
			GVAR(vehicle_numbers_guard) = [
				[[1,0], 1], // tanks
				[[1,0], 1], // apc (bmp)
				[[1,1], 1], // apc2 (brdm)
				[[1,1], 1], // jeep with mg (uaz mg)
				[[1,0], 1] // jeep with gl (uaz grenade)
			];
			GVAR(vehicle_numbers_guard_static) = [
				[[1,1], 1], // tanks
				[[1,1], 1], // apc (bmp)
				[[1,1], 1], // aa (shilka)
				[[5,4], 1], // Static MG
				[[3,2], 1] // Static Launcher
			];
			GVAR(vehicle_numbers_patrol) = [
				[[1,1], 1], // tanks
				[[1,1], 1], // apc (bmp)
				[[1,1], 1], // apc2 (brdm)
				[[1,1], 1], // jeep with mg (uaz mg)
				[[1,1], 1] // jeep with gl (uaz grenade)
			];

			// allmost the same like above
			// first element the max number of ai "foot" groups that will get spawned, second element minimum number (no number for vehicles in group necessary)
			GVAR(footunits_guard) = [
	#ifndef __TT__
				[1,1], // basic groups
				[1,1] // specop groups
	#else
				[2,1], // basic groups
				[2,1] // specop groups
	#endif
			];
			GVAR(footunits_patrol) = [
				[5,3], // basic groups
				[5,3] // specop groups
			];
			GVAR(footunits_guard_static) = [
				[1,1], // basic groups
				[1,0] // specop groups
			];
			GVAR(footunits_attack) = [
				[1,1], // basic groups
				[1,1] // specop groups
			];
			GVAR(vehicle_numbers_attack) = [
				[[1,1], 1], // tanks
				[[1,1], 1], // apc (bmp)
				[[1,1], 1], // apc2 (brdm)
				[[1,1], 1], // jeep with mg (uaz mg)
				[[1,1], 1], // jeep with gl (uaz grenade)
				[[1,1], 1] // Shilka/AA
			];
		};
		case 1: {
			GVAR(vehicle_numbers_guard) = [
				[[0,0], 1], // tanks
				[[1,0], 1], // apc (bmp)
				[[1,0], 1], // apc2 (brdm)
				[[1,1], 1], // jeep with mg (uaz mg)
				[[1,1], 1] // jeep with gl (uaz grenade)
			];
			GVAR(vehicle_numbers_guard_static) = [
				[[0,0], 1], // tanks
				[[1,0], 1], // apc (bmp)
				[[1,0], 1], // aa (shilka)
				[[5,4], 1], // Static MG
				[[3,2], 1] // Static Launcher
			];
			GVAR(vehicle_numbers_patrol) = [
				[[0,0], 1], // tanks
				[[1,1], 1], // apc (bmp)
				[[1,0], 1], // apc2 (brdm)
				[[2,1], 1], // jeep with mg (uaz mg)
				[[1,1], 1] // jeep with gl (uaz grenade)
			];

			// allmost the same like above
			// first element the max number of ai "foot" groups that will get spawned, second element minimum number (no number for vehicles in group necessary)
			GVAR(footunits_guard) = [
	#ifndef __TT__
				[2,1], // basic groups
				[2,1] // specop groups
	#else
				[3,1], // basic groups
				[3,1] // specop groups
	#endif
			];
			GVAR(footunits_patrol) = [
				[6,3], // basic groups
				[6,3] // specop groups
			];
			GVAR(footunits_guard_static) = [
				[3,1], // basic groups
				[2,1] // specop groups
			];
			GVAR(footunits_attack) = [
				[3,1], // basic groups
				[2,1] // specop groups
			];
			GVAR(vehicle_numbers_attack) = [
				[[0,0], 1], // tanks
				[[1,0], 1], // apc (bmp)
				[[1,0], 1], // apc2 (brdm)
				[[1,1], 1], // jeep with mg (uaz mg)
				[[1,1], 1], // jeep with gl (uaz grenade)
				[[0,0], 1] // Shilka/AA
			];
		};
		case 2: {
			GVAR(vehicle_numbers_guard) = [
				[[0,0], 1], // tanks
				[[0,0], 1], // apc (bmp)
				[[0,0], 1], // apc2 (brdm)
				[[2,1], 1], // jeep with mg (uaz mg)
				[[2,1], 1] // jeep with gl (uaz grenade)
			];
			GVAR(vehicle_numbers_guard_static) = [
				[[0,0], 1], // tanks
				[[0,0], 1], // apc (bmp)
				[[0,0], 1], // aa (shilka)
				[[5,4], 1], // Static MG
				[[3,2], 1] // Static Launcher
			];
			GVAR(vehicle_numbers_patrol) = [
				[[0,0], 1], // tanks
				[[0,0], 1], // apc (bmp)
				[[0,0], 1], // apc2 (brdm)
				[[2,1], 1], // jeep with mg (uaz mg)
				[[2,1], 1] // jeep with gl (uaz grenade)
			];

			// allmost the same like above
			// first element the max number of ai "foot" groups that will get spawned, second element minimum number (no number for vehicles in group necessary)
			GVAR(footunits_guard) = [
	#ifndef __TT__
				[4,1], // basic groups
				[4,1] // specop groups
	#else
				[4,1], // basic groups
				[4,1] // specop groups
	#endif
			];
			GVAR(footunits_patrol) = [
				[8,3], // basic groups
				[6,3] // specop groups
			];
			GVAR(footunits_guard_static) = [
				[4,1], // basic groups
				[3,1] // specop groups
			];
			GVAR(footunits_attack) = [
				[6,1], // basic groups
				[6,1] // specop groups
			];
			GVAR(vehicle_numbers_attack) = [
				[[0,0], 1], // tanks
				[[0,0], 1], // apc (bmp)
				[[0,0], 1], // apc2 (brdm)
				[[2,1], 1], // jeep with mg (uaz mg)
				[[2,1], 1], // jeep with gl (uaz grenade)
				[[1,0], 1] // Shilka/AA
			];
		};
	};
	
	// enemy ai skill: [base skill, random value (random 0.3) that gets added to the base skill]
	GVAR(skill_array) = switch (GVAR(EnemySkill)) do {
		case 1: {[0.2,0.2]};
		case 2: {[0.4,0.3]};
		case 3: {[0.7,0.3]};
	};
	
	#ifndef __TT__
	// if set to true no enemy AI will attack base and destroy bonus vehicles or whatever
	GVAR(no_sabotage) = (GVAR(WithBaseAttack) == 1);
	#endif
};

if (isDedicated) exitWith {};

if (GVAR(with_ai)) then {
	GVAR(current_ai_num) = 0;
};

if (GVAR(with_ranked)) then {
	GVAR(ranked_a) = [
		20, // points that an engineer must have to repair/refuel a vehicle
		[3,2,1,0], // points engineers get for repairing an air vehicle, tank, car, other
#ifndef __TT__
		10, // points an artillery operator needs for a strike
#else
		1,
#endif
		3, // points in the AI version for recruiting one soldier
		10, // points a player needs for an AAHALO parajump
#ifndef __TT__
		10, // points that get subtracted for creating a vehicle at a MHQ
#else
		1,
#endif
		20, // points needed to create a vehicle at a MHQ
		3, // points a medic gets if someone heals at his Mash
		["Sergeant","Lieutenant","Captain","Major"], // Ranks needed to drive different vehicles, starting with: kindof wheeled APC, kindof Tank, kindof Helicopter (except the inital 4 helis), Plane
		30, // points that get added if a player is xxx m in range of a main target when it gets cleared
		400, // range the player has to be in to get the main target extra points
		10, // points that get added if a player is xxx m in range of a sidemission when the sidemission is resolved
		200, // range the player has to be in to get the sidemission extra points
		20, // points needed for an egineer to rebuild the support buildings at base
		10, // points needed to build a mg nest
		5, // points needed in AI Ranked to call in an airtaxi
		20, // points needed to call in an air drop
		4, // points a medic gets when he heals another unit
		1, // points that a player gets when transporting others
		20, // points needed for activating satellite view
		20, // points needed to build a FARP (engineer)
		2 // points a player gets for reviving another player
	];

	// distance a player has to transport others to get points
	GVAR(transport_distance) = 500;

	// rank needed to fly the wreck lift chopper
	GVAR(wreck_lift_rank) = "CAPTAIN";
} else {
	GVAR(ranked_a) = [];
};

GVAR(graslayer_index) = if (GVAR(GrasAtStart) == 1) then {0} else {1};

GVAR(disable_viewdistance) = (GVAR(ViewdistanceChange) == 1);

#ifdef __OWN_SIDE_OPFOR__
_armor = if (GVAR(LockArmored) == 1) then {
	switch (true) do {
		default {["B_APC_Tracked_01_AA_F","B_APC_Tracked_01_rcws_F","B_MBT_01_cannon_F","B_MBT_01_arty_F","B_MBT_01_mlrs_F","B_APC_Wheeled_01_cannon_F"]};
	};
} else {[]};
_car = if (GVAR(LockCars) == 1) then {
	switch (true) do {
		default {["B_MRAP_01_gmg_F","B_MRAP_01_hmg_F","B_MRAP_01_F"]};
	};
} else {[]};
#endif
#ifdef __OWN_SIDE_BLUFOR__
_armor = if (GVAR(LockArmored) == 1) then {
	switch (true) do {
		default {["O_MBT_02_arty_F","O_APC_Tracked_02_cannon_F","O_MBT_02_cannon_F","O_APC_Tracked_02_AA_F","O_APC_Wheeled_02_rcws_F"]};
	};
} else {[]};
_car = if (GVAR(LockCars) == 1) then {
	switch (true) do {
		default {["O_MRAP_02_gmg_F","O_MRAP_02_hmg_F","O_MRAP_02_F"]};
	};
} else {[]};
#endif
#ifdef __OWN_SIDE_INDEPENDENT__
_armor = if (GVAR(LockArmored) == 1) then {["BMP3","BTR90","BTR90_HQ","GAZ_Vodnik","GAZ_Vodnik_HMG"]} else {[]};
_car = if (GVAR(LockCars) == 1) then {["UAZ_RU","UAZ_AGS30_RU","D30_RU"]} else {[]};
#endif
#ifdef __TT__
_armor = if (GVAR(LockArmored) == 1) then {
	["T55_TK_GUE_EP1","T34_TK_GUE_EP1","BRDM2_TK_GUE_EP1","BTR40_MG_TK_GUE_EP1","Ural_ZU23_TK_GUE_EP1"]
} else {[]};
_car = if (GVAR(LockCars) == 1) then {
	["Offroad_DSHKM_TK_GUE_EP1","Offroad_SPG9_TK_GUE_EP1","D30_TK_GUE_EP1"]
} else {[]};
#endif

if (count _armor > 0) then {GVAR(helilift1_types) = [GVAR(helilift1_types), _armor] call FUNC(arrayPushStack2)};
if (count _car > 0) then {GVAR(helilift1_types) = [GVAR(helilift1_types), _car] call FUNC(arrayPushStack2)};

{GVAR(helilift1_types) set [_forEachIndex, toUpper _x]} forEach GVAR(helilift1_types);

#ifdef __TT__
{
	switch (_x select 1) do {
		case 0: {_x set [count _x, GVAR(helilift1_types)]};
		case 1: {_x set [count _x, GVAR(heli_wreck_lift_types)]};
	};
} forEach GVAR(choppers_blufor);
{
	switch (_x select 1) do {
		case 0: {_x set [count _x, GVAR(helilift1_types)]};
		case 1: {_x set [count _x, GVAR(heli_wreck_lift_types)]};
	};
} forEach GVAR(choppers_blufor);
#else
{
	switch (_x select 1) do {
		case 0: {_x set [count _x, GVAR(helilift1_types)]};
		case 1: {_x set [count _x, GVAR(heli_wreck_lift_types)]};
	};
} forEach GVAR(choppers);
// also possible:
// _elem = GVAR(choppers) select 2; // third chopper
// _elem set [3, GVAR(helilift_types_custom)];
#endif

GVAR(show_chopper_welcome) = GVAR(WithChopHud);

if (GVAR(with_ai)) then {
	// additional AI recruit buildings
	// these have to be placed in the editor, give them a var name in the editor
	// only client handling means, no damage handling done for those buildings (contrary to the standard AI hut)
	// example:
	// GVAR(additional_recruit_buildings) = [my_ai_building1, my_ai_building2];
	GVAR(additional_recruit_buildings) = [];
};

// TYRGHEN
if (GVAR(with_target_markers) == 1 && GVAR(hasParamsArray)) then {
	GVAR(strongpointmarker) = "";
	GVAR(towerpointmarker) = "";
};

_allowrecompile = getNumber(missionConfigFile/"allowFunctionsRecompile") == 1;
if (!GVAR(with_ranked)) then {
	FUNC(weaponcargo) = if (_allowrecompile) then {
		compile preprocessFileLineNumbers "x_client\x_weaponcargo_a3.sqf";
	} else {
		compileFinal preprocessFileLineNumbers "x_client\x_weaponcargo_a3.sqf";
	};
} else {
	FUNC(weaponcargo) = if (_allowrecompile) then {
		compile preprocessFileLineNumbers "x_client\x_weaponcargor_a3.sqf";
	} else {
		compileFinal preprocessFileLineNumbers "x_client\x_weaponcargor_a3.sqf";
	};
};